<template>
  <div>
    <el-card class="mb-10">
      <CategorySelector ref="caregory"></CategorySelector>
    </el-card>
    <el-card>
      <div v-if="!isEdit">
        <el-button
          class="mt-10"
          type="primary"
          icon="el-icon-plus"
          @click="isEdit = true"
          >添加属性</el-button
        >
        <el-table border class="mt-10" :data="attrInfor">
          <el-table-column
            label="序号"
            width="80"
            align="center"
            type="index"
          ></el-table-column>
          <el-table-column
            label="属性名"
            width="160"
            prop="attrName"
          ></el-table-column>
          <el-table-column label="属性值列表">
            <template #default="{ row, $index }">
              <el-tag
                class="mr-5 mb-5"
                type="success"
                v-for="attrVal in row.attrValueList"
                :key="attrVal.id"
              >
                {{ attrVal.valueName }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="140">
            <template #default="{ row, $index }">
              <el-button
                type="warning"
                size="small"
                icon="el-icon-edit-outline"
                @click="onEdit(row)"
              ></el-button
              >&nbsp;&nbsp;
              <el-popconfirm
                :title="`确认要删除[${row.attrName}]吗?`"
                @confirm="deleteAttr(row)"
              >
                <template #reference>
                  <el-button
                    type="danger"
                    size="small"
                    icon="el-icon-delete"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-else>
        <el-form inline>
          <el-form-item label="属性名">
            <el-input
              placeholder="输入属性名"
              v-model="attrForm.attrName"
            ></el-input>
          </el-form-item>
        </el-form>
        <div class="mb-10">
          <el-button type="primary" @click="addAttrValue">添加属性</el-button>
          <el-button @click="onCancel">取消</el-button>
        </div>
        <el-table border :data="attrForm.attrValueList">
          <el-table-column
            label="序号"
            type="index"
            width="80"
            align="center"
          ></el-table-column>
          <el-table-column label="属性值名称" key="slot">
            <template v-slot="{ row, $index }">
              <el-input
                v-if="!row.inputVisiable"
                placeholder="请输入属性名"
                ref="inputRef"
                v-model="row.valueName"
                @blur="onBlur(row, $index)"
              ></el-input>
              <div v-else @click="onDiv(row)">
                {{ row.valueName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="80">
            <template #default="{ row, $index }">
              <el-button
                type="danger"
                size="small"
                icon="el-icon-delete"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="mt-10">
          <el-button type="primary" @click="addAttrList">保存</el-button>
          <el-button @click="onCancel">取消</el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import CategorySelector from "@/components/CategorySelector/index.vue";
import { mapActions, mapState } from "vuex";
import { cloneDeep } from "lodash";
export default {
  name: "Attr",
  data() {
    return {
      isEdit: false, //切换添加平台属性
      attrForm: {
        attrName: "",
        attrValueList: [],
        categoryId: "",
        categoryLevel: 3,
      },
      category3: "",
    };
  },
  components: {
    CategorySelector,
  },
  methods: {
    ...mapActions("attr", [
      "getgetcaregoryListAll",
      "deleteAttrInfo",
      "onSavew",
    ]),
    //添加新的属性值
    addAttrValue() {
      this.attrForm.attrValueList.push({
        valueName: "",
        inputVisible: true,
      });
      // 等模板渲染完成再执行
      this.$nextTick(() => {
        this.$refs.inputRef.focus();
      });
    },

    //失焦事件
    onBlur(row, index) {
      if (!row.valueName.trim()) {
        this.attrForm.attrValueList.splice(index, 1);
      }
      // 重复判断，不判断自身
      this.$set(row, "inputVisiable", true);
    },
    // 点击div
    onDiv(row) {
      this.$set(row, "inputVisiable", false);
      // row.inputVisiable = true;
      // 等模板渲染完成再执行
      this.$nextTick(() => {
        this.$refs.inputRef.focus();
      });
    },
    //聚焦事件
    showInput() {
      row.inputVisible = false;
      this.$nextTick(() => {
        console.log(this.$refs);
      });
    },
    //取消按钮的回调
    onCancel() {
      this.isEdit = false;
      //清空数据
      this.attrForm = {
        attrName: "",
        attrValueList: [],
        categoryId: "",
        categoryLevel: 3,
      };
    },
    // //点击保存添加平台属性
    addAttrList(){
      this.isEdit = false;
      this.attrForm.categoryId = this.category3Id
      this.onSavew(this.attrForm)
      this.attrForm = {
        attrName:'',
        attrValueList: [],
        categoryId: "",
        categoryLevel: 3,
      }
    },
    // addAttrList(){
    //   this.attrForm.categoryId = this.$refs.caregoryRef.category3;
    //   this.isEdit = false;
    //   this.getAddAttrList(this.attrForm);
    //    // 清空数据
    //    this.formData = {
    //     attrName: "", //添加的平台属性
    //     attrValueList: [], //添加平台属性值的数组
    //     categoryId: "", //子组件的三级分类id
    //     categoryLevel: 3, //添加添加平台属性的参数,
    //   };
    //   let { category1, category2, category3 } = this.$refs.categoryRef;
    //   console.log(category1, category2, category3);
    //   this.onSavew({
    //     category1Id: category1,
    //     category2Id: category2,
    //     category3Id: category3,
    //   });
    // },
    //并删除
    deleteAttr(row) {
      this.deleteAttrInfo(row.id);
      const { category1Id, category2Id, category3Id } = this.$refs.caregory;
      this.getgetcaregoryListAll({ category1Id, category2Id, category3Id });
    },
    onEdit(row) {
      this.isShow = true;
      this.formData = cloneDeep(row);
    },
  },
  computed: {
    ...mapState("attr", ["attrInfor", "category3Id"]),
  },
};
</script>

<style scoped>
</style>